package org.yoao.crm.business.impl;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.yoao.crm.bean.StaticBean;
import org.yoao.crm.business.service.IConfigService;
import org.yoao.crm.dao.CompanyDAO;
import org.yoao.crm.dao.CustomerDAO;
import org.yoao.crm.dao.CustomerTypeDAO;
import org.yoao.crm.dao.MenuDAO;
import org.yoao.crm.dao.RoleDAO;
import org.yoao.crm.dao.SignupDAO;
import org.yoao.crm.dao.UserDAO;
import org.yoao.crm.tools.FileTool;

/**
 * @author yxwonline.com
 * @description 
 * @date 2010-8-1 下午11:21:49
 */
public class ConfigServiceImpl implements IConfigService {
	private static String crmPath = ConfigServiceImpl.class.getResource("/") + "resources/crm.properties";
	
	public ConfigServiceImpl(){
		if(crmPath != null && crmPath.startsWith("file:/")) crmPath = crmPath.substring(6);
		FileTool fileTool = new FileTool(crmPath);
		if(!fileTool.exists()){
			fileTool.create();
		}
	}
	
	@Override
	public Map init() throws Exception {
		Properties property = new Properties();
		property.clear();
		InputStream in = new BufferedInputStream(new FileInputStream(crmPath));
		property.load(in);
		in.close();
		
		Map result = new HashMap();
		String jndiName = property.getProperty(StaticBean.JNDINAME);
		String pageSize = property.getProperty(StaticBean.PAGESIZE);
		String isShowOldCustomer = property.getProperty(StaticBean.ISSHOWOLDCUSTOMER);
		
		result.put(StaticBean.JNDINAME, jndiName);
		result.put(StaticBean.PAGESIZE, pageSize);
		result.put(StaticBean.ISSHOWOLDCUSTOMER, isShowOldCustomer);
		
		return result;
	}
	
	@Override
	public String initDataBase() throws Exception {
		StringBuffer result = new StringBuffer();
		result.append(new RoleDAO().create());
		result.append("</BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		result.append(new CompanyDAO().create());
		result.append("</BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		result.append(new UserDAO().create());
		result.append("</BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		result.append(new CustomerDAO().create());
		result.append("</BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		result.append(MenuDAO.getMenuDAO().create());
		result.append("</BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		result.append(new SignupDAO().create());
		result.append("</BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		result.append(new CustomerTypeDAO().create());
		return result.toString();
	}

	@Override
	public boolean saveConfig(String jndiName, String pageSize, String isShowOldCustomer) throws Exception {
		Properties property = new Properties();
		FileInputStream in = new FileInputStream(crmPath);
		property.load(in);
		in.close();
		if(jndiName != null && !jndiName.trim().equals("")){
			property.setProperty(StaticBean.JNDINAME, jndiName);
		}
		if(pageSize != null && !pageSize.trim().equals("")){
			property.setProperty(StaticBean.PAGESIZE, pageSize);
		}
		if(isShowOldCustomer == null || isShowOldCustomer.trim().equals("")){
			isShowOldCustomer = "0";
		}
		property.setProperty(StaticBean.ISSHOWOLDCUSTOMER, isShowOldCustomer);
		
		FileOutputStream out = new FileOutputStream(crmPath);
		property.store(out, null);
		out.flush();
		out.close();
		
		StaticBean.JNDINAME_VALUE = jndiName;
		System.out.println("重置配置JNDI:" + StaticBean.JNDINAME_VALUE);
		try{
			StaticBean.PAGESIZE_VALUE = Integer.parseInt(pageSize);
		}catch(Exception e){
			StaticBean.PAGESIZE_VALUE = 20;
			e.printStackTrace();
		}
		System.out.println("重置配置单页显示记录数:" + StaticBean.PAGESIZE_VALUE);
		try {
			if("1".equals(isShowOldCustomer))
				StaticBean.ISSHOWOLDCUSTOMER_VALUE = true;
			else
				StaticBean.ISSHOWOLDCUSTOMER_VALUE = false;
		} catch (Exception e) {
			StaticBean.ISSHOWOLDCUSTOMER_VALUE = false;
			e.printStackTrace();
		}		
		System.out.println("重置配置是否启用客户旧信息:" + StaticBean.ISSHOWOLDCUSTOMER_VALUE);
		
		return true;
	}
}